package cn.tedu.baking.controller;

import cn.tedu.baking.mapper.ContentMapper;
import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.entity.Content;
import cn.tedu.baking.pojo.vo.ContentIndexVO;
import cn.tedu.baking.pojo.vo.ContentManagementVO;
import cn.tedu.baking.pojo.vo.ContentUpdateVO;
import cn.tedu.baking.pojo.vo.UserVO;
import cn.tedu.baking.response.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 稿件相关的请求处理类
 */
@RestController
@RequestMapping("/v1/contents/")
public class ContentController {
    public static void main(String[] args) {


    }
    @Autowired
    ContentMapper contentMapper;
    @GetMapping("{type}/management")
    public JsonResult management(@PathVariable Integer type, HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        List<ContentManagementVO> contentManagementVOS = contentMapper.selectContentByTypeAndUserId(type, user.getId());
        return JsonResult.ok(contentManagementVOS);
    }

    @PostMapping("add-new")
    public  JsonResult addNew(@RequestBody ContentDTO contentDTO,HttpSession session){
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        UserVO user = (UserVO) session.getAttribute("user");
        if(content.getId() != null){//如果稿件信息中存在id值,说明此次动作是修改
            content.setUpdateTime(new Date());//更新修改时间
            content.setUpdateBy(user.getId());//更新的用户(id)
            contentMapper.updateContentInfo(content);//更新
        }
        else {  //否则是新增
            content.setCreateTime(new Date());
            contentMapper.insertContent(content);
        }
        return JsonResult.ok();
    }
    @GetMapping("{type}/{categoryId}/index")
    public JsonResult selectContentByTypeAndCategoryId(@PathVariable Integer type,
                                                       @PathVariable Long categoryId){
        List<ContentIndexVO> contentIndexVOS = contentMapper.selectContentByTypeAndCategoryId(type, categoryId);
        return JsonResult.ok(contentIndexVOS);
    }

    @GetMapping("{type}/list")
    public JsonResult list(@PathVariable Integer type){
        return  JsonResult.ok(contentMapper.selectContentByType(type));
    }

    @GetMapping("{id}/update")
    public JsonResult update(@PathVariable Long id){
        return JsonResult.ok(contentMapper.selectUpdateInfoById(id));
    }

    @PostMapping("{id}/delete")
    public  JsonResult delete(@PathVariable Long id){
        //删除稿件   稿件有不同的类型,例如文章/咨询(图片资源)   视频(视频资源)
        ContentUpdateVO contentUpdateVO = contentMapper.selectUpdateInfoById(id);
        new File("d:/file"+contentUpdateVO.getImgUrl()).delete();//删除图片资源
        if(contentUpdateVO.getType() == 2){//如果稿件类型是2  说明是视频的稿件
            new File("d:/file"+contentUpdateVO.getVideoUrl()).delete();//删除视频资源
        }
        contentMapper.deleteContentById(id);
        return JsonResult.ok();
    }

    @GetMapping("{id}/detail")
    public  JsonResult detail(@PathVariable Long id){
        contentMapper.updateViewCountById(id);//浏览量增加1
        return JsonResult.ok(contentMapper.selectContentDetailById(id));
    }
    @GetMapping("{userId}/{contentId}/other")
    public JsonResult other(@PathVariable Long userId,
                            @PathVariable Long contentId){
        return JsonResult.ok(contentMapper.selectContentOtherInfoByUserId(userId,contentId));
    }
    @GetMapping("hot")
    public JsonResult hot(){
        return JsonResult.ok(contentMapper.selectContentHot());
    }
}
